#include "delge_squirrelfunctions.h"

#include "s_enums.h"

#include "s_texture.h"
#include "s_sprite.h"
#include "s_camera.h"
#include "s_mesh.h"
#include "s_model.h"
#include "s_font.h"
#include "s_graphics.h"

#include "s_music.h"
#include "s_sound.h"
#include "s_audio.h"

SQRESULT s_RegisterFunctions (HSQUIRRELVM v)
{
	SquirrelVM::CreateFunctionGlobal(s_Quit,							_SC("Quit"),							_SC(""));
	SquirrelVM::CreateFunctionGlobal(s_CreateGraphicsWindow,			_SC("CreateGraphicsWindow"),			_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_DestroyGraphicsWindow,			_SC("DestroyGraphicsWindow"),			_SC(""));
	SquirrelVM::CreateFunctionGlobal(s_RenderCurrentScreen,				_SC("RenderCurrentScreen"),				_SC(""));
	SquirrelVM::CreateFunctionGlobal(s_UpdateCurrentScreen,				_SC("UpdateCurrentScreen"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_AddScreen,						_SC("AddScreen"),						_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_ChangeScreen,					_SC("ChangeScreen"),					_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_SetScreen,						_SC("SetScreen"),						_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_HasFocus,						_SC("HasFocus"),						_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_HideMouse,						_SC("HideMouse"),						_SC(""));
	SquirrelVM::CreateFunctionGlobal(s_ShowMouse,						_SC("ShowMouse"),						_SC(""));

	s_Graphics_RegisterFunctions			(SquirrelVM::GetVMPtr());
	s_Graphics_Texture_RegisterFunctions	(SquirrelVM::GetVMPtr());
	s_Graphics_Mesh_RegisterFunctions		(SquirrelVM::GetVMPtr());
	s_Graphics_Model_RegisterFunctions		(SquirrelVM::GetVMPtr());
	s_Graphics_Camera_RegisterFunctions		(SquirrelVM::GetVMPtr());
	s_Graphics_Sprite_RegisterFunctions		(SquirrelVM::GetVMPtr());
	s_Input_RegisterFunctions				(SquirrelVM::GetVMPtr());
	s_Audio_RegisterFunctions				(SquirrelVM::GetVMPtr());
	s_Audio_Music_RegisterFunctions			(SquirrelVM::GetVMPtr());
	s_Audio_Sound_RegisterFunctions			(SquirrelVM::GetVMPtr());

	SquirrelVM::RunScript(SquirrelVM::CompileBuffer(s_enums));

	SquirrelVM::RunScript(SquirrelVM::CompileBuffer(s_texture));
	SquirrelVM::RunScript(SquirrelVM::CompileBuffer(s_sprite));
	SquirrelVM::RunScript(SquirrelVM::CompileBuffer(s_camera));
	SquirrelVM::RunScript(SquirrelVM::CompileBuffer(s_mesh));
	SquirrelVM::RunScript(SquirrelVM::CompileBuffer(s_model));
	SquirrelVM::RunScript(SquirrelVM::CompileBuffer(s_font));
	SquirrelVM::RunScript(SquirrelVM::CompileBuffer(s_graphics));

	SquirrelVM::RunScript(SquirrelVM::CompileBuffer(s_music));
	SquirrelVM::RunScript(SquirrelVM::CompileBuffer(s_sound));
	SquirrelVM::RunScript(SquirrelVM::CompileBuffer(s_audio));

	return SQ_OK;
}

SQInteger s_Quit (HSQUIRRELVM v)
{
	DelGEEvent oEvent;
	oEvent.eType = DelGE_Event_Game_Quit;
	oEvent.GameData.pData = NULL;
	EventManager::PushEvent(oEvent);

	return 0;
}

SQInteger s_CreateGraphicsWindow (HSQUIRRELVM v)
{
	const SQChar			*sTitle;
	SQInteger				iWidth, iHeight, iBitDepth;
	SQBool					bFullscreen;

	sq_getstring			(v, 2, &sTitle);
	sq_getinteger			(v, 3, &iWidth);
	sq_getinteger			(v, 4, &iHeight);
	sq_getinteger			(v, 5, &iBitDepth);
	sq_getbool				(v, 6, &bFullscreen);

	Globals::pGame->CreateGraphicsWindow(string(sTitle), iWidth, iHeight, iBitDepth, (bool)bFullscreen);

	return 0;
}

SQInteger s_DestroyGraphicsWindow (HSQUIRRELVM v)
{
	Globals::pGame->DestroyGraphicsWindow();

	return 0;
}

SQInteger s_AddScreen (HSQUIRRELVM v)
{
	const SQChar			*sScreenName;
	string					screenPath;

	sq_getstring			(v, 2, &sScreenName);

	screenPath = "scripts/screens/";
	screenPath += sScreenName;
	screenPath += ".nut";

	Globals::vScreens.push_back(sScreenName);

	SquirrelObject s_Screen;

	try
	{
		s_Screen = SquirrelVM::CompileScript(screenPath.c_str());
		SquirrelVM::RunScript(s_Screen);
		s_Screen.Reset();	
	}
	catch (SquirrelError& error)
	{
		cout << error.desc << endl;
		s_Quit(v);
	}


	return 0;
}

SQInteger s_ChangeScreen (HSQUIRRELVM v)
{
	const SQChar			*sScreenName;

	sq_getstring			(v, 2, &sScreenName);

	if (Globals::pScreen->sSquirrelName != "")
		Globals::pScreen->Unload();
	Globals::pScreen->sSquirrelName = string(sScreenName);
	Globals::pScreen->Load();

	return 0;
}

SQInteger s_SetScreen (HSQUIRRELVM v)
{
	const SQChar			*sScreenName;

	sq_getstring			(v, 2, &sScreenName);

	Globals::pScreen->sSquirrelName = string(sScreenName);

	return 0;
}

SQInteger s_RenderCurrentScreen	(HSQUIRRELVM v)
{
	if (Globals::pScreen->sSquirrelName != "")
		Globals::pGame->RenderCurrentScreen();

	return 0;
}

SQInteger s_UpdateCurrentScreen	(HSQUIRRELVM v)
{
	SQFloat					fDelta;

	sq_getfloat				(v, 2, &fDelta);

	if (Globals::pScreen->sSquirrelName != "")
		Globals::pGame->UpdateCurrentScreen(fDelta);

	return 0;
}

SQInteger s_HasFocus (HSQUIRRELVM v)
{
	sq_pushbool(v, ((SQBool)Globals::pGame->GetEngine()->GetWindowHandle()->HasFocus()));

	return 1;
}

SQInteger s_HideMouse (HSQUIRRELVM v)
{
	Globals::pGame->HideMouse();

	return 0;
}

SQInteger s_ShowMouse (HSQUIRRELVM v)
{
	Globals::pGame->ShowMouse();

	return 0;
}